package dao

import (
	"context"
	"wxpay/internal/model"
	"wxpay/pkg/merrors"

	"github.com/nsqio/go-nsq"

	"gorm.io/gorm"
)

type ProfitReturn interface {
	Create(ctx context.Context, tx *gorm.DB, pfs *model.Profitreturn) (err error)
	QueryByOutReturnNo(ctx context.Context, db *gorm.DB, outReturnNo string) (order *model.Profitreturn, err error)
	SyncSuccess(ctx context.Context, tx *gorm.DB, pfs *model.Profitreturn) (err error)
	SyncFailed(ctx context.Context, tx *gorm.DB, pfs *model.Profitreturn) (err error)
	SyncProcessing(ctx context.Context, tx *gorm.DB, pfs *model.Profitreturn) (err error)
}

type ProfitReturnRepo struct {
	*gorm.DB
	producer *nsq.Producer
}

func NewProfitReturnRepo(db *gorm.DB, p *nsq.Producer) *ProfitReturnRepo {
	return &ProfitReturnRepo{
		DB:       db,
		producer: p,
	}
}

// 创建回退记录
func (p ProfitReturnRepo) Create(_ context.Context, tx *gorm.DB, pfs *model.Profitreturn) (err error) {
	const op merrors.Op = "ProfitReturnRepo.Create"
	if e := tx.Create(pfs).Error; e != nil {
		return merrors.E(op, merrors.DBError, e, merrors.ErrInternal)
	}
	return nil
}

func (p ProfitReturnRepo) QueryByOutReturnNo(_ context.Context, db *gorm.DB, outReturnNo string) (order *model.Profitreturn, err error) {
	const op merrors.Op = "ProfitReturnRepo.QueryByOutReturnNo"
	res := model.Profitreturn{}
	if err := db.Model(&model.Profitreturn{}).Where("out_return_no = ?", outReturnNo).First(&res).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil, merrors.E(op, merrors.NotExist, merrors.ErrNotFound, err)
		} else {
			return nil, merrors.E(op, merrors.DBError, merrors.ErrInternal, err)
		}
	}
	return &res, nil
}

// 更新到回退成功
func (p ProfitReturnRepo) SyncSuccess(_ context.Context, tx *gorm.DB, pfs *model.Profitreturn) (err error) {
	const op merrors.Op = "ProfitReturnRepo.SyncSuccess"
	if e := tx.Model(pfs).Updates(map[string]interface{}{
		"result":      pfs.Result,
		"finish_time": pfs.FinishTime,
		"update_time": pfs.UpdateTime,
	}).Where("id = ?", pfs.ID).Error; e != nil {
		err = merrors.E(op, merrors.DBError, e, merrors.ErrInternal)
		return
	}
	return
}

// 更新到回退失败
func (p ProfitReturnRepo) SyncFailed(_ context.Context, tx *gorm.DB, pfs *model.Profitreturn) (err error) {
	const op merrors.Op = "ProfitReturnRepo.SyncFailed"
	if e := tx.Model(pfs).Updates(map[string]interface{}{
		"result":      pfs.Result,
		"fail_reason": pfs.FailReason,
		"update_time": pfs.UpdateTime,
	}).Where("id = ?", pfs.ID).Error; e != nil {
		err = merrors.E(op, merrors.DBError, e, merrors.ErrInternal)
		return
	}
	return
}

// 更新到回退处理中
func (p ProfitReturnRepo) SyncProcessing(_ context.Context, tx *gorm.DB, pfs *model.Profitreturn) (err error) {
	const op merrors.Op = "ProfitReturnRepo.SyncProcessing"
	if e := tx.Model(pfs).Updates(map[string]interface{}{
		"result":      pfs.Result,
		"update_time": pfs.UpdateTime,
	}).Where("id = ?", pfs.ID).Error; e != nil {
		err = merrors.E(op, merrors.DBError, e, merrors.ErrInternal)
		return
	}
	return
}
